package zoo{
	import flash.utils.Timer;
	import flash.utils.setInterval;

	public class VirtualPet
	{
		private static var maxNameLength:int = 20;
		private static var maxCalories:int = 2000;
		private static var caloriesPerSecond:int = 100;
		private var petName;
		private var currentCalories:Number = VirtualPet.maxCalories/2;
		private var creationTime;
		private var digestIntervalID;
		
		public function VirtualPet(name)
		{
			setName(name);
			this.creationTime = new Date();
			digestIntervalID = setInterval(digest, 1000);
		}
		internal function setName(newName)
		{
			if(newName.lenght > maxNameLength) {
				newName = newName.substr(0 , maxNameLength);
			}else if (newName == "") {
				return
			}
			petName = newName;
		}
		internal function getName()
		{
			return petName;
		}
		internal function eat(numberOfCalories:int)
		{
			var newCurrentCalories = currentCalories + numberOfCalories;
			if(newCurrentCalories > 2000) {
				currentCalories = 2000;
			}else {
				currentCalories = newCurrentCalories;
			}
			trace(getName() + "ate some fvood. It now has " + currentCalories + " calories remainig.");
		}
		private function digest()
		{
			currentCalories -= VirtualPet.caloriesPerSecond;
			trace(getName() + " digested some food. It now has " + currentCalories + " calories remaining.");
		}
		internal function getHunger()
		{
			return currentCalories / 2000;
		}
		internal function getAge()
		{
			var currentTime = new Date();
			var age:Number = currentTime.time - this.creationTime.time;
			
			return age;
		}

	}
}